

using System;
using System.Collections;
using System.Diagnostics;


using CStock;



namespace CStock
{
	/// <summary>
	/// Summary description for Orderes.
	/// </summary>
	public class Orders : ArrayList, IOrders
	{
        string iD;

		#region Constructor
        public Orders()
		{
		}
		#endregion

		#region IOrderes

        /// <summary>
        /// 
        /// </summary>
        public IOrder this[string myID]
        {
            get
            {
                for (int i = 0; i < this.Count; i++)
                {
                    IOrder myPf = this[i] as IOrder;
                    //if( myPf.ID == name)
                    if (myPf.TraderID == myID)
                    {
                        return myPf;
                    }
                }
                return null;
            }
        }
        //sorts the input list after name
        public bool SortAfterBid()
        {
            try
            {
                SortedList sortedListOfNames = new SortedList();
                for (int i = 0; i < this.Count; i++)
                {
                    object c = this[i].Bid;
                    if (!sortedListOfNames.ContainsValue(c))
                        sortedListOfNames.Add(c.ToString(), c);
                }
                ArrayList arrListResult = new ArrayList();
                IList sortedList = sortedListOfNames.GetValueList();
                for (int i = 0; i < sortedList.Count; i++)
                {
                    //arrListResult.Add(this[j]);
                    double fVal = Convert.ToDouble(sortedList[i]);
                    for (int j = 0; j < this.Count; j++)
                    {
                        if (fVal == this[j].Bid)
                            arrListResult.Add(this[j]);
                    }

                }
                this.Clear();
                for (int i = 0; i < arrListResult.Count; i++)
                {
                    this.Add(arrListResult[i]);
                }
                
                return true;

            }
            catch (Exception exc)
            {
                Debug.Assert(false, "Error sorting array list", exc.Message);

            }
            return false;


        }
		public new IOrder this[int index]
		{
			get
			{
				try
				{
					if ((index >= 0) && (index < base.Count))
					{
						object o = base[index];
						return o as IOrder;
					}
					else
					{
						return null;
					}
				}
				catch
				{
					Debug.Assert(false,"error getting IOrder");
					return null;
				}
			}
			set
			{
				if ((index >= 0) && (index < base.Count))
				{
					base[index] = value;
				}
			}
		}
        public string ID
        {
            get
            {

                return this.iD;
            }
            set
            {

                this.iD = value;
            }
        }
		public override int Add(object obj)
		{
			try
			{
				Order objOrder = (Order)obj;
				if (this.Count == 0)
				{
					//Empty Collection -> First Element
					base.Add(objOrder);
					return 0;
				}
				else
				{
					base.Add(objOrder);
					return 0;
				}
			}
			catch (Exception exc)
			{
				throw exc;
			}
			
		}
		public override void Remove(object obj)
		{
			Order objOrder=(Order)obj;
			if (this.Contains(objOrder))
			{
				base.Remove(objOrder);
			}
		}

        public double Value_Orders(DateTime dateCurrent)
        {
            double val = 0;
            for (int i = 0; i < this.Count; i++)
            {
                IOrder ord = this[i];
                val += ord.GetOrderValue(dateCurrent);

            }
            return val;
        }
        
        #endregion
    }
}
